package cn.com.czpb.client.module.news.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;

import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.news.HighRiseBean;
import cn.com.czpb.client.model.news.HighriseEntity;
import cn.com.czpb.client.module.news.adapter.HighRiseAdapter;
import cn.com.czpb.client.module.news.view.HighriseTopView;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.ParamUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class HighriseActivity extends MToolBarActivity implements OnRefreshListener,OnLoadmoreListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.recyclerview_highrise)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_layout)
    SmartRefreshLayout swipe_layout;
    private boolean mFlagRefresh = true;//刷新还是加载
    private int page = 1;
    private int pageSize = 15;
    private HighRiseAdapter adapter;

    @Override
    public void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new HighRiseAdapter(null);
        recyclerView.setAdapter(adapter);
        swipe_layout.setRefreshHeader(new ClassHeader_other(this));
        swipe_layout.setOnRefreshListener(this);
        adapter.addHeaderView(HighriseTopView.instance(this).getView());
        adapter.setOnItemChildClickListener(this);
        getData();
    }
    @OnClick(R.id.ll_back)
    public void goBack(View view){
        activity.onBackPressed();
    }
    //获取高层数据
    private void getData() {
        LogUtils.i("init", "getNewslist");
        Map<String, String> map = new HashMap<>();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("Page", page + "");
        map.put("PageSize", pageSize + "");
        Factory.provideHttpService()
                .highriselist(ParamUtils.getRequestParam(map))
                .subscribeOn(Schedulers.io())
                .filter(new Func1<HighriseEntity, Boolean>() {
                    @Override
                    public Boolean call(HighriseEntity entity) {
                        if ("200".equals(entity.code)) {
                            LogUtils.i("200--list>成功");
                            return true;
                        }
                        LogUtils.i("200--list>失败");
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HighriseEntity>() {
                    @Override
                    public void call(HighriseEntity entity) {
                        if ("200".equals(entity.code)) {
                            setData(entity.data);

                        } else {
//                            TUtils.toast(entity.msg);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        adapter.loadMoreFail();
                    }
                });
    }

    private void setData(List<HighRiseBean> list) {
        if (mFlagRefresh){
            adapter.setNewData(list);
        }else{
            adapter.addData(list);
        }
    }
    @Override
    protected int setLayoutId() {
        return R.layout.activity_highrise;
    }
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        mFlagRefresh = true;
        swipe_layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipe_layout.finishRefresh();
            }
        }, 2000);
        swipe_layout.setLoadmoreFinished(false);
        getData();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        HighRiseBean bean = (HighRiseBean) adapter.getItem(position);

        PageCtrl.start2HighriseDetialActivity(this,bean);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page ++;
        mFlagRefresh = true;
        getData();
        swipe_layout.setLoadmoreFinished(true);
    }
}
